package com.sztx.pay.center.rpc.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSONObject;
import com.sztx.pay.center.common.annotation.SlaveQueryAnnotation;
import com.sztx.pay.center.common.util.Constance.UserActType;
import com.sztx.pay.center.common.util.JFUtils;
import com.sztx.pay.center.common.util.ValidateUtil;
import com.sztx.pay.center.dataaccess.dao.hbase.DumpOrderHbaseDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.UserActFlowMysqlDAO;
import com.sztx.pay.center.dataaccess.domain.HbaseQueryDO;
import com.sztx.pay.center.dataaccess.domain.UserActFlowDO;
import com.sztx.pay.center.dataaccess.domain.UserActFlowDumpDO;
import com.sztx.pay.center.rpc.api.domain.UserActFlowHbaseRequest;
import com.sztx.pay.center.rpc.api.domain.UserActFlowHbaseResponse;
import com.sztx.pay.center.rpc.api.domain.UserActFlowRequest;
import com.sztx.pay.center.rpc.api.domain.UserActFlowResponse;
import com.sztx.pay.center.rpc.api.domain.UserActFlowResponse.UserActFlow;
import com.sztx.pay.center.rpc.api.service.UserActFlowRpcService;
import com.sztx.se.common.util.StringUtil;
import com.sztx.se.common.util.date.DateUtil;
import com.sztx.se.core.service.impl.BaseServiceImpl;
import com.sztx.se.dataaccess.mysql.config.PageQuery;
@Service
public class UserActFlowRpcServiceImpl extends BaseServiceImpl implements UserActFlowRpcService{
	
	@Autowired
	UserActFlowMysqlDAO userActFlowMysqlDAO;
	
	@Autowired
	private DumpOrderHbaseDAO dumpOrderHbaseDAO;
	
	@Override
	@SlaveQueryAnnotation
	public UserActFlowResponse findUserActFlow(UserActFlowRequest request) {
		ValidateUtil.notEmpty(request.getUserId(), "userId不能为空");
		ValidateUtil.notNull(request.getStartDate(), "startDate不能为空");
		ValidateUtil.notNull(request.getEndDate(), "endDate不能为空");
		ValidateUtil.notNull(request.getPageNo(), "pageNo不能为空");
		ValidateUtil.notNull(request.getPageSize(), "pageSize不能为空");
		ValidateUtil.notEmpty(request.getChannelId(), "channelId不能为空");
		
		int actType;
		if(JFUtils.isJFChannel(request.getChannelId())){
			actType = UserActType.JF.getCode();
		}else{
			actType = UserActType.CASH.getCode();
		}
		
		
		Integer inOrOut = request.getInOrOut();
		String startTime = DateUtil.formatDate(request.getStartDate(), DateUtil.LONG_DATE_FORMAT_STR);
		String endTime = DateUtil.formatDate(request.getEndDate(), DateUtil.LONG_DATE_FORMAT_STR);
		
		UserActFlowDO userActFlowDO = new UserActFlowDO();
		userActFlowDO.setUserId(request.getUserId());
		userActFlowDO.setBusinessType(request.getBusinessType());
		userActFlowDO.setActType(actType);
		userActFlowDO.setOriginOrderNo(request.getOriginOrderNo());
		userActFlowDO.setTradeType(request.getTradeType());
		
		List<String> businessTypeList = null;
		String businessType = request.getBusinessType();
		if(StringUtil.isNotBlank(request.getBusinessType())){
			String[] businessTypeArr = businessType.split(",");
			businessTypeList = Arrays.asList(businessTypeArr);
		}
		
		
		Integer counts = userActFlowMysqlDAO.listCountByTime(userActFlowDO,startTime,endTime,inOrOut,businessTypeList);
		List<UserActFlowDO> userActFlowDOS = null;
		UserActFlowResponse response = new UserActFlowResponse();
		if(counts >= 1){
			PageQuery pageQuery = new PageQuery(request.getPageNo(), request.getPageSize());
			userActFlowDOS = userActFlowMysqlDAO.listPageByTime(userActFlowDO, pageQuery,startTime,endTime,inOrOut,businessTypeList);
			
			Integer inAmount = userActFlowMysqlDAO.inAmount(userActFlowDO,  startTime, endTime, inOrOut, businessTypeList);
			Integer outAmount = userActFlowMysqlDAO.outAmount(userActFlowDO, startTime, endTime, inOrOut, businessTypeList);
			response.setInAmount(inAmount);
			response.setOutAmount(outAmount);
		}else{
			response.setPageNo(request.getPageNo());
			response.setPageSize(request.getPageSize());
			response.setTotal(0);
			return response;
		}
		
		response.setPageNo(request.getPageNo());
		response.setPageSize(request.getPageSize());
		response.setTotal(counts);
		List<UserActFlow> userActFlowS = new ArrayList<UserActFlow>();
		for (UserActFlowDO tmpuserActFlowDO : userActFlowDOS) {
			UserActFlow userActFlow = new UserActFlowResponse().new UserActFlow();
			userActFlow.setUserId(tmpuserActFlowDO.getUserId());
			userActFlow.setTradeOrderNo(tmpuserActFlowDO.getTradeOrderNo());
			userActFlow.setOriginOrderNo(tmpuserActFlowDO.getOriginOrderNo());
			userActFlow.setInnerRemark(tmpuserActFlowDO.getInnerRemark());
			userActFlow.setUserRemark(tmpuserActFlowDO.getUserRemark());
			userActFlow.setActNo(tmpuserActFlowDO.getActNo());
			userActFlow.setActType(tmpuserActFlowDO.getActType());
			userActFlow.setActBalance(tmpuserActFlowDO.getActBalance());
			userActFlow.setFreezeAmount(tmpuserActFlowDO.getFreezeAmount());
			userActFlow.setFreezeBalance(tmpuserActFlowDO.getFreezeBalance());
			userActFlow.setTradeTime(tmpuserActFlowDO.getTradeTime());
			userActFlow.setTradeType(tmpuserActFlowDO.getTradeType());
			userActFlow.setTranAmount(tmpuserActFlowDO.getTranAmount());
			userActFlow.setBusinessType(tmpuserActFlowDO.getBusinessType());
			userActFlow.setOtherUserId(tmpuserActFlowDO.getOtherUserId());
			userActFlow.setOtherRealName(tmpuserActFlowDO.getOtherRealName());
			userActFlow.setOtherActType(tmpuserActFlowDO.getOtherActType());
			userActFlowS.add(userActFlow);
		}
		response.setUserActFlowS(userActFlowS);
		return response;
	}


	@Override
	public UserActFlowHbaseResponse findHbaseUserActFlow(UserActFlowHbaseRequest request) {
		ValidateUtil.notEmpty(request.getUserId(), "userId不能为空");
		ValidateUtil.notNull(request.getStartDate(), "startDate不能为空");
		ValidateUtil.notNull(request.getEndDate(), "endDate不能为空");
		ValidateUtil.notNull(request.getPageNo(), "pageNo不能为空");
		ValidateUtil.notNull(request.getPageSize(), "pageSize不能为空");
		ValidateUtil.notEmpty(request.getChannelId(), "channelId不能为空");
		
		int actType;
		if(JFUtils.isJFChannel(request.getChannelId())){
			actType = UserActType.JF.getCode();
		}else{
			actType = UserActType.CASH.getCode();
		}
		
		
		Integer inOrOut = request.getInOrOut();
		String startTime = DateUtil.formatDate(request.getStartDate(), DateUtil.LONG_DATE_FORMAT_STR);
		String endTime = DateUtil.formatDate(request.getEndDate(), DateUtil.LONG_DATE_FORMAT_STR);
		
		String businessType = request.getBusinessType();
		String startBusinessType = null;
		String endBusinessType = null;
		if(StringUtil.isNotBlank(request.getBusinessType())){
			String[] businessTypeArr = businessType.split(",");
			startBusinessType = businessTypeArr[0];
			endBusinessType = businessTypeArr[businessTypeArr.length-1];
		}
		
		HbaseQueryDO hbaseQueryDO = dumpOrderHbaseDAO.scanActFlow(request.getNextPageStartRowKey(), request.getOriginOrderNo(), startTime, endTime, request.getUserId(), actType, startBusinessType, request.getPageSize(), request.getTradeType(), inOrOut, endBusinessType);
		UserActFlowHbaseResponse response = new UserActFlowHbaseResponse();
		List<UserActFlow> userActFlowS = new ArrayList<UserActFlowResponse.UserActFlow>();
		List<String> jsonDataList = hbaseQueryDO.getJsonDataList();
		if (CollectionUtils.isEmpty(jsonDataList)) return response;
		
		for (String string : jsonDataList) {
			UserActFlowDumpDO userActFlowDumpDO = JSONObject.parseObject(string, UserActFlowDumpDO.class);
			UserActFlowResponse.UserActFlow userActFlow = new UserActFlowResponse().new UserActFlow();
			userActFlow.setUserId(userActFlowDumpDO.getUserId());
			userActFlow.setTradeOrderNo(userActFlowDumpDO.getTradeOrderNo());
			userActFlow.setOriginOrderNo(userActFlowDumpDO.getOriginOrderNo());
			userActFlow.setInnerRemark(userActFlowDumpDO.getInnerRemark());
			userActFlow.setUserRemark(userActFlowDumpDO.getUserRemark());
			userActFlow.setActNo(userActFlowDumpDO.getActNo());
			userActFlow.setActType(userActFlowDumpDO.getActType());
			userActFlow.setActBalance(userActFlowDumpDO.getActBalance());
			userActFlow.setFreezeAmount(userActFlowDumpDO.getFreezeAmount());
			userActFlow.setFreezeBalance(userActFlowDumpDO.getFreezeBalance());
			userActFlow.setTradeTime(userActFlowDumpDO.getTradeTime());
			userActFlow.setTradeType(userActFlowDumpDO.getTradeType());
			userActFlow.setTranAmount(userActFlowDumpDO.getTranAmount());
			userActFlow.setBusinessType(userActFlowDumpDO.getBusinessType());
			userActFlow.setOtherUserId(userActFlowDumpDO.getOtherUserId());
			userActFlow.setOtherRealName(userActFlowDumpDO.getOtherRealName());
			userActFlow.setOtherActType(userActFlowDumpDO.getOtherActType());
			userActFlowS.add(userActFlow);
		}
		response.setUserActFlowS(userActFlowS);
		response.setHasNextPage(hbaseQueryDO.getHasNextPage());
		response.setNextPageStartRowKey(hbaseQueryDO.getNextPageStratRowKey());
		return response;
	}

}
